package com.facebook.imagepipeline.platform;

import android.annotation.TargetApi;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.BitmapFactory;
import android.graphics.BitmapFactory.Options;
import android.support.v4.util.Pools.SynchronizedPool;
import com.facebook.common.internal.Preconditions;
import com.facebook.common.internal.VisibleForTesting;
import com.facebook.common.references.CloseableReference;
import com.facebook.common.streams.LimitedInputStream;
import com.facebook.common.streams.TailAppendingInputStream;
import com.facebook.imagepipeline.image.EncodedImage;
import com.facebook.imagepipeline.memory.BitmapPool;
import com.facebook.imageutils.BitmapUtil;
import java.io.InputStream;
import java.nio.ByteBuffer;
import javax.annotation.concurrent.ThreadSafe;

@TargetApi(21)
@ThreadSafe
public class ArtDecoder
  implements PlatformDecoder
{
  private static final int DECODE_BUFFER_SIZE = 16384;
  private static final byte[] EOI_TAIL = { -1, -39 };
  private final BitmapPool mBitmapPool;

  @VisibleForTesting
  final Pools.SynchronizedPool<ByteBuffer> mDecodeBuffers;

  public ArtDecoder(BitmapPool paramBitmapPool, int paramInt, Pools.SynchronizedPool paramSynchronizedPool)
  {
    this.mBitmapPool = paramBitmapPool;
    this.mDecodeBuffers = paramSynchronizedPool;
    for (int i = 0; i < paramInt; i++)
      this.mDecodeBuffers.release(ByteBuffer.allocate(16384));
  }

  private static BitmapFactory.Options getDecodeOptionsForStream(EncodedImage paramEncodedImage, Bitmap.Config paramConfig)
  {
    BitmapFactory.Options localOptions = new BitmapFactory.Options();
    localOptions.inSampleSize = paramEncodedImage.getSampleSize();
    localOptions.inJustDecodeBounds = true;
    BitmapFactory.decodeStream(paramEncodedImage.getInputStream(), null, localOptions);
    if ((localOptions.outWidth == -1) || (localOptions.outHeight == -1))
      throw new IllegalArgumentException();
    localOptions.inJustDecodeBounds = false;
    localOptions.inDither = true;
    localOptions.inPreferredConfig = paramConfig;
    localOptions.inMutable = true;
    return localOptions;
  }

  public CloseableReference<Bitmap> decodeFromEncodedImage(EncodedImage paramEncodedImage, Bitmap.Config paramConfig)
  {
    BitmapFactory.Options localOptions = getDecodeOptionsForStream(paramEncodedImage, paramConfig);
    int i;
    if (localOptions.inPreferredConfig != Bitmap.Config.ARGB_8888)
      i = 1;
    try
    {
      while (true)
      {
        CloseableReference localCloseableReference = decodeStaticImageFromStream(paramEncodedImage.getInputStream(), localOptions);
        return localCloseableReference;
        i = 0;
      }
    }
    catch (RuntimeException localRuntimeException)
    {
      if (i != 0)
        return decodeFromEncodedImage(paramEncodedImage, Bitmap.Config.ARGB_8888);
      throw localRuntimeException;
    }
  }

  public CloseableReference<Bitmap> decodeJPEGFromEncodedImage(EncodedImage paramEncodedImage, Bitmap.Config paramConfig, int paramInt)
  {
    boolean bool = paramEncodedImage.isCompleteAt(paramInt);
    BitmapFactory.Options localOptions = getDecodeOptionsForStream(paramEncodedImage, paramConfig);
    InputStream localInputStream = paramEncodedImage.getInputStream();
    Preconditions.checkNotNull(localInputStream);
    if (paramEncodedImage.getSize() > paramInt);
    for (Object localObject1 = new LimitedInputStream(localInputStream, paramInt); ; localObject1 = localInputStream)
    {
      if (!bool);
      for (Object localObject2 = new TailAppendingInputStream((InputStream)localObject1, EOI_TAIL); ; localObject2 = localObject1)
      {
        int i;
        if (localOptions.inPreferredConfig != Bitmap.Config.ARGB_8888)
          i = 1;
        try
        {
          while (true)
          {
            CloseableReference localCloseableReference = decodeStaticImageFromStream((InputStream)localObject2, localOptions);
            return localCloseableReference;
            i = 0;
          }
        }
        catch (RuntimeException localRuntimeException)
        {
          if (i != 0)
            return decodeFromEncodedImage(paramEncodedImage, Bitmap.Config.ARGB_8888);
          throw localRuntimeException;
        }
      }
    }
  }

  protected CloseableReference<Bitmap> decodeStaticImageFromStream(InputStream paramInputStream, BitmapFactory.Options paramOptions)
  {
    Preconditions.checkNotNull(paramInputStream);
    int i = BitmapUtil.getSizeInByteForBitmap(paramOptions.outWidth, paramOptions.outHeight, paramOptions.inPreferredConfig);
    Bitmap localBitmap1 = (Bitmap)this.mBitmapPool.get(i);
    if (localBitmap1 == null)
      throw new NullPointerException("BitmapPool.get returned null");
    paramOptions.inBitmap = localBitmap1;
    ByteBuffer localByteBuffer = (ByteBuffer)this.mDecodeBuffers.acquire();
    if (localByteBuffer == null)
      localByteBuffer = ByteBuffer.allocate(16384);
    Bitmap localBitmap2;
    try
    {
      paramOptions.inTempStorage = localByteBuffer.array();
      localBitmap2 = BitmapFactory.decodeStream(paramInputStream, null, paramOptions);
      this.mDecodeBuffers.release(localByteBuffer);
      if (localBitmap1 != localBitmap2)
      {
        this.mBitmapPool.release(localBitmap1);
        localBitmap2.recycle();
        throw new IllegalStateException();
      }
    }
    catch (RuntimeException localRuntimeException)
    {
      this.mBitmapPool.release(localBitmap1);
      throw localRuntimeException;
    }
    finally
    {
      this.mDecodeBuffers.release(localByteBuffer);
    }
    return CloseableReference.of(localBitmap2, this.mBitmapPool);
  }
}

/* Location:           C:\Users\user\Desktop\dd28_pcdd720\classes_dex2jar.jar
 * Qualified Name:     com.facebook.imagepipeline.platform.ArtDecoder
 * JD-Core Version:    0.6.2
 */